package com.glory.shsi1rpt;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 数据处理工具类
 *
 * @ProjectName: shcleanrpt
 * @ClassName: DataUtil
 * @Author: Ivan
 * @Date: 2019-06-26 17:04
 * @Version 1.0
 */
public class DataUtil {

	/**
	 * 保留两位小数,进行四舍五入
	 *
	 * @Author Ivan
	 * @Date 2019/6/26
	 * @Params
	 * @Return java.lang.Double
	 */
	public static Double saveOneBitTwoRound(Double d) {
		BigDecimal bd = new BigDecimal(d);
		Double res = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return res;
	}

	/**
	 * 去重
	 *
	 * @Author Ivan
	 * @Date 2019/7/11
	 * @Params
	 * @Return java.util.List
	 */
	public static List removeDuplicate(List list) {
		HashSet h = new HashSet(list);
		list.clear();
		list.addAll(h);
		return list;
	}

	/**
	 * 字符串分隔，适用于多批次查询
	 *
	 * @Author Ivan
	 * @Date 2019/9/18
	 * @Params
	 * @Return java.lang.String
	 */
	public static String listToStr(String data) {
		String res = "";
		try {
			if (StringUtils.isNotEmpty(data)) {
				StringBuffer sb = new StringBuffer();
				data = replace(data).replace("，", ",");
				String[] str = data.split(",");
				for (String s : str) {
					sb.append(s).append(",");
				}
				res = sb.toString();
				res = res.substring(0, res.length() - 1);
			}
		} catch (Exception e) {
			System.out.println("字符分割异常" + e.getMessage());
		}
		return res;
	}

	/**
	 * 去除回车、空格
	 *
	 * @Author Ivan
	 * @Date 2019/9/20
	 * @Params
	 * @Return java.lang.String
	 */
	public static String replace(String str) {
		String destination = "";
		if (str != null) {
			Pattern pattern = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = pattern.matcher(str);
			destination = m.replaceAll("");
		}
		return destination;
	}

	public static List<String> getBetweenDates(Date startTime, Date endTime) {
		List<String> result = new ArrayList<String>();
		Calendar tempStart = Calendar.getInstance();
		tempStart.setTime(startTime);
		tempStart.add(Calendar.DAY_OF_YEAR, 1);
		Calendar tempEnd = Calendar.getInstance();
		tempEnd.setTime(endTime);
		SimpleDateFormat sdf;
		int h = 8, m = 30;
		while (tempStart.before(tempEnd)) {
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			result.add(sdf.format(tempStart.getTime()));
			tempStart.add(Calendar.DAY_OF_YEAR, 1);
		}
		return result;
	}

	public static List<String> getTimeLag5Minute() {
		ArrayList<String> list = new ArrayList<String>();
		for (int h = 8, m = 30; h < 24; h += 1) {
//			if (m >= 60) {
//				h++;
//				m = 0;
//			}
			if (h >= 24) {
				break;
			}
			String hour = String.valueOf(h);
			String minute = String.valueOf(m);
			if (hour.length() < 2) {
				hour = "0" + hour;
			}
			if (minute.length() < 2) {
				minute = "0" + minute;
			}
			list.add(hour + ":" + minute);
		}

		return list;
	}

	public static void main(String args[]) {
//		System.out.println(saveOneBitTwoRound(2.123456));
//		System.out.println(listToStr("F-P8N0044DAW,\n" +
//				"F-P8P0076BAW,\n" +
//				"F-P8P0078BAW,\n" +
//				"F-P8P0096BAW,\n" +
//				"F-P8P0097BAW,\n" +
//				"\n" +
//				"F-P8N0112BAW,\n" +
//				"F-P8N0267AAW,\n" +
//				"F-P8N0276AAW"));
		List<String> list = getTimeLag5Minute();
		for (String s : list) {
			System.out.println(s);
		}
	}
}